var swfu;
$(function() {
	if (swfu_settings) {
		var settings = {
			button_placeholder_id : "add-placeholder",
			button_image_url : "/admin/backend/resources/images/add_sprite.png", 
			button_width : 16, 
			button_height : 16, 
			button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES, 
			button_disabled : false, 
			button_cursor : SWFUpload.CURSOR.HAND, 
			button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT, 
			
			
			flash_url : "/admin/backend/resources/js/swfupload/Flash/swfupload.swf",
			upload_url: swfu_settings.upload_url,
			post_params: swfu_settings.post_params,
			file_size_limit : swfu_settings.file_size_limit,
			file_types : swfu_settings.file_types,
			file_types_description : swfu_settings.file_types_description,
			file_upload_limit : 100,
			file_queue_limit : 0,
			debug: false,
			
			file_queued_handler : function(obj) {
			},
			file_queue_error_handler : function(obj, code, message) {
			},
			file_dialog_complete_handler : function(selected, queued, total) {
				this.webtacular_total    = queued;
				this.webtacular_complete = 0;
				this.startUpload();
			},
			upload_start_handler : function(obj) {
				if (this.webtacular_complete == 0) {
					$.blockUI({ message: $('#uploadprogress'), css: { width: 123, border: 0, background: 'transparent', left: ($(window).width()/2)-62, textAlign: 'left' } });
					 $("#uploadprogress .progressbar").progressBar({ showText: false });
				}
			},
			upload_progress_handler : function(obj, complete, total) {
				$('#uploadprogress .progressbar').progressBar(Math.floor((100 / this.webtacular_total * this.webtacular_complete) + (100/total*complete/this.webtacular_total)));
			},
			upload_error_handler : function(obj, code, message) {
			},
			upload_success_handler : function(obj, data) {
			},
			upload_complete_handler : function(obj) {
				this.webtacular_complete++
				if (this.webtacular_complete<this.webtacular_total) {
					this.startUpload();
				} else {
					$('#uploadprogress .progressbar').progressBar(100);
					$.unblockUI();
				}
				$('.filter').val('');
				$.get('?paging[order]=id+desc&paging[filter]=');
			}
		};
		if ($('#add-placeholder').length) swfu = new SWFUpload(settings);	
	}
	
	$('#uploadprogress a.cancel').livequery('click', function(e) {
		e.preventDefault();
		swfu.cancelQueue();
		$.unblockUI();
	});
});
